package com.jzo2o.health.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.DateUtils;
import com.jzo2o.health.mapper.ReservationSettingMapper;
import com.jzo2o.health.mapper.UserMapper;
import com.jzo2o.health.model.domain.ReservationSetting;
import com.jzo2o.health.model.domain.User;
import com.jzo2o.health.model.dto.request.ReservationSettingUpsertReqDTO;
import com.jzo2o.health.model.dto.response.ReservationDateResDTO;
import com.jzo2o.health.model.dto.response.ReservationSettingResDTO;
import com.jzo2o.health.service.IReservationSettingService;
import com.jzo2o.health.service.IUserService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @Author lyh
 * @Date 2024/12/13 20:52
 */
@Service
public class IReservationSettingServiceImpl extends ServiceImpl<ReservationSettingMapper, ReservationSetting> implements IReservationSettingService {
     @Autowired
     private IReservationSettingService owner;
    /**
     * 按月查询预约设置
     * @param date
     * @return
     */
    @Override
    public List<ReservationSettingResDTO> getReservationSettingByMonth(String date) {
        LocalDate now = LocalDate.parse(date+"-01");
        LocalDate monthBegin = DateUtils.getMonthBegin(now);
        LocalDate monthEnd = now.with(TemporalAdjusters.lastDayOfMonth());
        List<ReservationSettingResDTO> list =  this.getBaseMapper().getReservationSettingByMonth(monthBegin,monthEnd);
        return list;
    }

    @Override
    public void editNumberByDate(ReservationSettingUpsertReqDTO dto) {
        if(dto==null){
            throw new BadRequestException("参数异常");
        }
        //填写的预约人数需要大于0，小于1000
        if(dto.getNumber()<=0||dto.getNumber()>1000){
            throw new BadRequestException("预约人数需要大于0，小于1000");
        }
        //查询是否存在
        ReservationSetting one = this.lambdaQuery().eq(ReservationSetting::getOrderDate, dto.getOrderDate())
                .one();
        if(one==null){
            ReservationSetting reservationSetting = BeanUtils.copyBean(dto, ReservationSetting.class);
            reservationSetting.setReservations(0);
            this.save(reservationSetting);
        }else{
            if(dto.getNumber()<one.getReservations()){
                throw new BadRequestException("预约人数需要大于等于已预约人数");
            }
          this.lambdaUpdate()
                  .set(ReservationSetting::getOrderDate,dto.getOrderDate())
                  .set(ReservationSetting::getNumber,dto.getNumber())
                  .eq(ReservationSetting::getId,one.getId()).update();
        }
    }

    @Override
    public void updateFile(MultipartFile file) {
        ExcelReader reader= null;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
            List<ReservationSettingUpsertReqDTO> list=reader.readAll(ReservationSettingUpsertReqDTO.class);
            if(list==null){
                throw new BadRequestException("文件上传异常");
            }
            for (ReservationSettingUpsertReqDTO reservationSetting : list) {
                owner.editNumberByDate(reservationSetting);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 按月查询可预约日期
     * @param month
     * @return
     */
    @Override
    public ReservationDateResDTO getReservationDateByMonth(String month) {
        LocalDate now = LocalDate.parse(month+"-01");
        LocalDate monthBegin = DateUtils.getMonthBegin(now);
        LocalDate monthEnd = now.with(TemporalAdjusters.lastDayOfMonth());
        List<String> dateList=this.getBaseMapper().getReservationDateByMonth(monthBegin,monthEnd);
        ReservationDateResDTO dto = new ReservationDateResDTO();
        dto.setDateList(dateList);
        return dto;
    }
}
